import { position as positionStore } from "@/store/position";
import { userData as userDataStore } from "@/store/userData";
import { updateUserGps } from "@/api/api";
import permision from "@/utils/permission.ts"



export const getPosition = async(askauth:boolean,reason:string) => {
    const position: any = positionStore();
    const userData: any = userDataStore();
    let requestNum = 0;
    /* #ifdef H5 */
    const obj = {
        longitude:108.04345,
        latitude:33.07234,
        id:userData.id,
    }


    uni.$emit('positionend')
    position.setPosition(obj);
    uni.positionend=true;
    if(userData.id){
        updateUserGps(obj)
    }
    /*#endif*/





    /*#ifdef APP-PLUS*/

    if(askauth){

        let platform = uni.getSystemInfoSync().platform;

        if(platform==='ios'){
            const auth = permision.judgeIosPermission('location');

            let str = reason||'当前无位置使用权限';


            if(!auth){
                uni.showModal({
                    title: `无法获取您的位置`,
                    content: `${str}，建议前往系统设置，允许应用访问[位置]`,
                    showCancel: true,
                    confirmText: "前往系统设置",
                    cancelText: "取消",
                    async success({ confirm, cancel }) {
                        if (confirm) {
                            permision.gotoAppPermissionSetting();
                        }
                    },
                });
                return;
            }
        }


        if (platform === "android") {
            const res = await permision
                .requestAndroidPermission("android.permission.ACCESS_FINE_LOCATION")
                .catch((err) => {
                });
    
            let str = "当前无位置使用权限";
    
            if (res !== 1) {
                uni.showModal({
                    title: `无法获取您的位置`,
                    content: `${str}，建议前往系统设置，允许应用访问[位置]`,
                    showCancel: true,
                    confirmText: "前往系统设置",
                    cancelText: "取消",
                    async success({ confirm, cancel }) {
                        if (confirm) {
                            permision.gotoAppPermissionSetting();
                        }
                    },
                });
                return;
            }
        }

    }


    uni.getLocation({
            type: "wgs84",
            async success(res) {
                console.log("当前位置的经度：" + res.longitude);
                console.log("当前位置的纬度：" + res.latitude);
                const longitude = res.longitude;
                const latitude = res.latitude;
                position.setPosition({longitude,latitude});
                uni.positionend = true;
                uni.$emit('positionend')
                if(!userData.id){
                    return
                }
                updateUserGps({longitude,latitude,id:userData.id})

            },
            fail(err){

                uni.positionend=true;
                uni.$emit('positionend')
                if(requestNum<3){
                    requestNum++;
                    setTimeout(()=>{
                        getPosition();
                    },2000)

                }
            }
    });
    /*#endif*/
};